<?php
/**
 * 增加考核计划
*/
apf_require_controller("Abstract");
apf_require_class("Bll_Jx_Setting");
class Setting_SetMonthController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $curr_year = empty($params['year']) ? date('Y') : intval($params['year']);

        $this->set_bll = new Bll_Jx_Setting();

        $per_list = $this->set_bll->get_performance_year($curr_year);
        $all_month = $last_month = array();
        //取出这一次计划的最大月份,表单中
        if(!empty($per_list)){
            foreach($per_list as $val){
                $all_month[]=$val['s_month'];
                if(empty($last_month) && date('m')>=$val['s_month'] && date('m') <=$val['e_month']){
                    $last_month = $val;
                }
            }
        }
        //post,需要用到last_month
        if($this->request->is_post_method()){
            //先删除计划之后的月份,再添加
            if(!empty($params['chooseMonth'])){
                $end_month = $params['chooseYear'] > date('Y')  ? 0 : $last_month['e_month'];
                $this->set_bll->remove_review_month($params['chooseYear'],$end_month);
                foreach($params['chooseMonth'] as $k=>$val){
                    $end = $val;
                    if($val !=12 && !empty($params['chooseMonth'][$k+1])){//下一个
                        $end = $params['chooseMonth'][$k+1];
                        if($end > 1 && $end <12){
                            $end -= 1;
                        }
                    }else{
                        $end = 12;
                    }
                    $in_data = array(
                            'year'=>$params['chooseYear'],
                            'month'=>$val,
                            'reviewDay'=>$params['chooseYear'].($val<10 ? '0'.$val : $val).'01',
                            's_month'=>$val,
                            'e_month'=>$end
                            );
                    $this->set_bll-> insert_review_month($in_data);
                }
            }
            $this->response->redirect(Util_JxUrls::setting_url('SetMonth',array('year'=>$params['chooseYear'])));
        }

        $this->request->set_attribute('last_month', $last_month);
        $this->request->set_attribute('all_month', $all_month);
        $this->request->set_attribute('curr_year',$curr_year);
        return 'Setting_SetMonth';
    }
}